#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: generate_docs 9869 2010-11-07 17:33:31Z gawrilow $
#
#  Complete production cycle for automatic part of documentation
#

unless (@ARGV) {
   die "usage: polymake --script generate_docs [--no-extensions] OutDir AppName ... \n",
       "from the shell: script(\"generate_docs\", \"OutDir\");\n";
}

my $Wiki="http://www.opt.tu-darmstadt.de/polymake/doku.php";
my $docdir="$InstallTop/xml/documentation";

shift(@ARGV), local @lookup_applications=(), local @Core::Application::lookup=($InstallTop) if $ARGV[0] eq "--no-extensions";

my $outdir=shift @ARGV;

my @Apps=@ARGV ? @ARGV : known Core::Application;

File::Path::mkpath($outdir);
my $tmpdir=Tempfile->dir."/".Tempfile->unique_name;

script("extract_docs", '--wiki' => $Wiki, '--outsuffix' => '.html', '--outdir' => $tmpdir, @Apps);

my $pmdocns=doc_namespace();

my $writer=open_doc_file("version.xml");
$writer->startTag([ $pmdocns, "version" ]);
if ($DeveloperMode) {
   my ($rev)= `svnversion -n $InstallTop` =~ /(\d+)[A-Z]?$/;
   $writer->dataElement("revision", $rev);
} else {
   $writer->dataElement("release", $Version);
}
if (my @extensions=known Core::Extension) {
   $writer->startTag("extensions");
   $writer->dataElement("extension", $_) for @extensions;
   $writer->endTag;
}
$writer->endTag;
close_doc_file($writer);

sub run_xsltproc {
   my ($out, $stylesheet, $in)=@_;
   if (system("xsltproc", "--path", $tmpdir, "--stringparam", "Wiki", $Wiki, "-o", $out, "$docdir/$stylesheet.xsl", "$in.xml")) {
      die "Command 'xsltproc @_' returned with error code $?\n",
          "Temporary files preserved in $tmpdir\n";
   }
}

system("cp -p $docdir/entities/* $tmpdir/");

push(@Apps, "core");
foreach (@Apps) {
   run_xsltproc("$outdir/$_.html", "doc", $_);
}

$writer=open_doc_file("applications.xml");
$writer->startTag([ $pmdocns, "applications" ]);
$writer->emptyTag("file", name => "$_.xml") for @Apps;
$writer->endTag;
close_doc_file($writer);

run_xsltproc("$outdir/index.html",     "index",     "applications");
run_xsltproc("$tmpdir/all.xml",        "all",       "applications");
run_xsltproc("$outdir/doc_index.html", "doc_index", "all");

if ($DebugLevel) {
   warn_print("preserving temporary files in $tmpdir");
} else {
   File::Path::rmtree($tmpdir);
}

File::Path::mkpath("$outdir/images");
system("cp -p $docdir/images/*.png $outdir/images");
system("cp -p $docdir/*.js $docdir/*.css $outdir/");


# Local Variables:
# mode: perl
# cperl-indent-level: 3
# End:
